package com.lee.lxl.idcard.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.lee.lxl.idcard.R;
import com.lee.lxl.idcard.urls.Urls;
import com.zhy.autolayout.AutoLayoutActivity;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PhoneActivity extends AutoLayoutActivity {

    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.bt_phone_onclick)
    Button btPhoneOnclick;
    @Bind(R.id.tv_phone)
    TextView tvPhone;

    private AbHttpUtil mAbHttpUtil = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);
        ButterKnife.bind(this);

        mAbHttpUtil = AbHttpUtil.getInstance(this);
        mAbHttpUtil.setTimeout(10000);
        mAbHttpUtil.setEncode("UTF-8");

    }

    @OnClick(R.id.bt_phone_onclick)
    public void onClick() {
        // 绑定参数
        AbRequestParams params = new AbRequestParams();
        params.put("key", "34173e998fe4a21c4e81b74c531e14a9");
        params.put("phone", etPhone.getText().toString());
        mAbHttpUtil.get(Urls.PHONE_NOMBER, params, new AbStringHttpResponseListener() {
            @Override
            public void onSuccess(int i, String s) {
                Log.e("PhoneActivity", s);

                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.getString("resultcode").equals("200")) {
                        Toast.makeText(PhoneActivity.this, jsonObject.getString("reason"), Toast.LENGTH_SHORT).show();
                        JSONObject object = jsonObject.getJSONObject("result");
                        tvPhone.setText(object.getString("province").toString() + "\n" + object.getString("city").toString()
                                + "\n" + object.getString("company") + "\n" + object.getString("card"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onFailure(int i, String s, Throwable throwable) {
                Log.e("PhoneActivity", s);
            }
        });
    }
}
